const { FusesPlugin } = require('@electron-forge/plugin-fuses');
const { FuseV1Options, FuseVersion } = require('@electron/fuses');
const path = require('path');
const fs = require('fs-extra');

module.exports = {
  packagerConfig: {
    asar: true,
    icon: '/src/icon/001.ico', // no file extension required
    // afterCopy: async (buildPath, electronVersion, platform, arch) => {
    //   // // 假设要复制的可执行文件在 node_modules 中
    //   // const exeSourcePath = path.join(__dirname, 'node_modules', 'pdf-to-printer', 'dist','SumatraPDF-3.4.6-32.exe');
    //   // const exeDestPath = path.join(buildPath, 'resources', 'SumatraPDF-3.4.6-32.exe');

    //   // // 确保目标目录存在
    //   // await fs.ensureDir(path.dirname(exeDestPath));
    //   // // 复制文件
    //   // return fs.copy(exeSourcePath, exeDestPath);
    // },
  },
  rebuildConfig: {},
  makers: [
    {
      name: '@electron-forge/maker-squirrel',
      config: {},
    },
    {
      name: '@electron-forge/maker-zip',
      platforms: ['darwin'],
    },
    {
      name: '@electron-forge/maker-deb',
      config: {},
    },
    {
      name: '@electron-forge/maker-rpm',
      config: {},
    },
  ],
  plugins: [
    {
      name: '@electron-forge/plugin-auto-unpack-natives',
      config: {
        afterCopy: async (buildPath, electronVersion, platform, arch) => {
        console.log("🚀 ~ afterCopy: ~ buildPath:", buildPath)
      // // 假设要复制的可执行文件在 node_modules 中
      // const exeSourcePath = path.join(__dirname, 'node_modules', 'pdf-to-printer', 'dist','SumatraPDF-3.4.6-32.exe');
      // const exeDestPath = path.join(buildPath, 'resources', 'SumatraPDF-3.4.6-32.exe');

      // // 确保目标目录存在
      // await fs.ensureDir(path.dirname(exeDestPath));
      // // 复制文件
      // return fs.copy(exeSourcePath, exeDestPath);
    },
      },
    },
    {
      name: '@electron-forge/plugin-webpack',
      config: {
        mainConfig: './webpack.main.config.js',
        renderer: {
          config: './webpack.renderer.config.js',
          entryPoints: [
            {
              html: './src/index.html',
              js: './src/renderer.js',
              name: 'main_window',
              preload: {
                js: './src/preload.js',
              },
            },
          ],
        },
      },
    },
    // Fuses are used to enable/disable various Electron functionality
    // at package time, before code signing the application
    new FusesPlugin({
      version: FuseVersion.V1,
      [FuseV1Options.RunAsNode]: false,
      [FuseV1Options.EnableCookieEncryption]: true,
      [FuseV1Options.EnableNodeOptionsEnvironmentVariable]: false,
      [FuseV1Options.EnableNodeCliInspectArguments]: false,
      [FuseV1Options.EnableEmbeddedAsarIntegrityValidation]: true,
      [FuseV1Options.OnlyLoadAppFromAsar]: true,
    }),
  ],
  hooks: {
    postPackage: async (config, packageResult) => {
      console.log("🚀 ~ postPackage: ~ packageResult:", packageResult)
      
      const exeSourcePath = path.join(__dirname, 'node_modules', 'pdf-to-printer', 'dist','SumatraPDF-3.4.6-32.exe');
      const exeDestPath = path.join(packageResult.outputPaths[0], 'resources','app.asar.unpacked','.webpack','main', 'SumatraPDF-3.4.6-32.exe');
      await fs.ensureDir(path.dirname(exeDestPath));
      return fs.copy(exeSourcePath, exeDestPath);
    },
  },
};
